package Operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        //
        System.out.println("请输入要删除图书的名称");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        int index = -1;
        int currentUsedSize = bookList.getUsedSize();
        for (int i = 0; i < currentUsedSize; i++) {
            Book book = bookList.getBook(i);
            if(name.equals(book.getName())){
                index = i;
                break;
            }
        }

        if(index == -1){
            System.out.println("未找到相关图书");
            return;
        }

        for(int i = index; i < currentUsedSize-1;i++){
            Book book = bookList.getBook(i+1);
            bookList.setBook(i,book);
        }

        bookList.setBook(currentUsedSize-1,null);

        bookList.setUsedSize(currentUsedSize-1);

        System.out.println("删除成功");
    }
}
